socket.io 和 netty-socketio 结合实现命名空间的推送时,socket.io的版本必须使用 1.7.4 这个版本。不能使用 2.0.0 以上的版本。
1 2 3 4
| // https://mvnrepository.com/artifact/com.corundumstudio.socketio/netty-socketio compile group: 'com.corundumstudio.socketio', name: 'netty-socketio', version: '1.7.14'
https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.4/socket.io.min.js
|
server:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| @Test public void test() throws InterruptedException { Configuration config = new Configuration(); config.setHostname("localhost"); config.setPort(8899);
final SocketIOServer server = new SocketIOServer(config); server.start(); String uid = "1111"; String namespace = String.format("/%s_%s", "msg", uid); SocketIONamespace chat1namespace = server.addNamespace(namespace);
for (int i = 0; i < Integer.MAX_VALUE; i++) { Thread.sleep(2000); chat1namespace.getBroadcastOperations().sendEvent("message", 1); System.out.println("room size: " + chat1namespace.getAllClients().size()); }
server.stop(); }
|
client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js" type="text/javascript"></script> <script type="text/javascript" src="./socket.io.js"></script>
<style> body { padding:20px; } #console { overflow: auto; } .username-msg {color:orange;} .connect-msg {color:green;} .disconnect-msg {color:red;} .send-msg {color:#888} </style>
</head>
<body> <h4>Netty-socketio Demo Chat</h4> <br/>
<div id="console" class="well"> </div> 消息总数:<div id="msgnum">0</di> </body>
<script type="text/javascript">
var socket = io.connect('http://192.168.1.89:8899/message');
socket.on('connect', function() { output('<span class="connect-msg">Client has connected to the server!</span>'); });
socket.on('message', function(data) { var num = $("#msgnum").html(); num = parseInt(num) + data; $("#msgnum").html(num); }); socket.on('test', function(data) { $("#msgnum").html(data); }); socket.on('disconnect', function() { output('<span class="disconnect-msg">The client has disconnected!</span>'); }); function sendDisconnect() { socket.disconnect(); } function output(message) { var currentTime = "<span class='time'>" + new Date() + "</span>"; var element = $("<div>" + currentTime + " " + message + "</div>"); $('#console').prepend(element); } </script> </html>
|
参考
- socket.io
- Add support for socket.io-client 2
- netty-socketio使用namespace
- netty-socketio实时推送信息